<template>
  <div class="container">
    <el-dialog
      :visible="showDialog"
      :title="formData.id ? '修改文章' : '新增文章'"
      @close="onCancel"
    >
      <el-form label-width="100px" :model="formData" :rules="rules" ref="form">
        <el-form-item label="文章标题:" prop="title">
          <el-input size="small" placeholder="请输入文章标题" v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容:" prop="articleBody">
          <quill-editor
            v-model="formData.articleBody"
            ref="editor"
            :options="editorOption"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input placeholder="请输入视频地址" size="small" v-model="formData.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-button @click="onCancel">取消</el-button>
        <el-button type="primary" @click="btnOk">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor' // 调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { detail, add, update } from '@/api/hmmm/articles'
export default {
  components: { quillEditor },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: {
        articleBody: '',
        title: '',
        videoURL: null
      },
      editorOption: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote', 'code-block'], // 引用，代码块
            ['image', 'link'] // 上传图片
          ]
        }
      },
      rules: {
        articleBody: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getDetail (id) {
      const { data: res } = await detail({ id })
      console.log(res)
      this.formData = res
    },
    onCancel () {
      this.formData = {
        articleBody: '',
        title: '',
        videoURL: null
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async btnOk () {
      try {
        await this.$refs.form.validate()
        if (this.formData.id) {
          // 修改
          await update(this.formData)
        } else {
          // 新增
          await add(this.formData)
        }
        this.$message.success(`${this.formData.id ? '修改' : '添加'}成功`)
        this.$emit('update:showDialog', false)
        console.log(this.$parent.page.page)
        this.$parent.page.page = 1
        this.$parent.getList(this.$parent.page)
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>
::v-deep .ql-toolbar.ql-snow {
  padding: 0 5px;
}
::v-deep .ql-editor {
 min-height: 150px;
}
</style>
